﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Tp.Dto.Allgemein;
using System.ComponentModel;


namespace Tp.Dto.Stammdaten
{
	  public class OrganisationDto : InPcBase, IAbstractDto
	  {

			#region Members

			private bool isDirty = false;
			private bool hasValue;
			private bool canUndo = false;

			private string name;
			private string nameOld;
			private string prefix;
			private string prefixOld;
			private string name2;
			private string name2Old;
			private RechtsformDto rechtsform;
			private RechtsformDto rechtsformOld;
			private string suchbegriff;
			private string suchbegriffOld;
			private OrganisationTypeDto organisationType;
			private OrganisationTypeDto organisationTypeOld;
			private AddressDto adresse;
			private AddressDto adresseOld;
			private TelefonDto telefon;
			private TelefonDto telefonOld;
			private TelefonDto fax;
			private TelefonDto faxOld;
			private string email;
			private string emailOld;
			private string homepage;
			private string homepageOld;
			private string abteilung;
			private string abteilungOld;
            private bool istLieferant;
            private bool istLieferantOld;
            private bool istEinkauf;
            private bool istEinkaufOld;
            private string kontaktquelle;
            private string kontaktquelleOld;
            private string kontaktgeber;
            private string kontaktgeberOld;
            private string erstkontakt;
            private string erstkontaktOld;
            private string erstkontaktMa;
            private string erstkontaktMaOld;
            private DateTime erstkontaktDatum;
            private DateTime erstkontaktDatumOld;
            private string businesstyp;
            private string businesstypOld;
            private string organisationbeziehung;
            private string organisationbeziehungOld;

			#endregion

			#region Properties

			[DataMember]
			public int OrganisationID { get; set; }

			[DataMember]
			public string Prefix
			{
				  get { return this.prefix; }
				  set
				  {
						if (this.prefix == value)
						{
							  return;
						}

						if (this.prefix != value)
						{
							  this.IsDirty = true;
						}
						this.prefixOld = this.prefix;
						this.prefix = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Prefix);
				  }
			}

			[DataMember]
			public string Name
			{
				  get { return this.name; }
				  set
				  {
						if (this.name == value)
						{
							  return;
						}

						if (this.name != value)
						{
							  this.IsDirty = true;
						}
						this.nameOld = this.name;
						this.name = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Name);
				  }
			}

			[DataMember]
			public string Name2
			{
				  get { return this.name2; }
				  set
				  {
						if (this.name2 == value)
						{
							  return;
						}

						if (this.name2 != value)
						{
							  this.IsDirty = true;
						}
						this.nameOld = this.name2;
						this.name2 = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Name2);
				  }
			}

			[DataMember]
			public RechtsformDto Rechtsform
			{
				  get { return this.rechtsform; }
				  set
				  {
						if (this.rechtsform == value)
						{
							  return;
						}

						if (this.rechtsform != value)
						{
							  this.IsDirty = true;
						}
						this.rechtsformOld = this.rechtsform;
						this.rechtsform = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Rechtsform);
				  }
			}

			[DataMember]
			public string Suchbegriff
			{
				  get { return this.suchbegriff; }
				  set
				  {
						if (this.suchbegriff == value)
						{
							  return;
						}

						if (this.suchbegriff != value)
						{
							  this.IsDirty = true;
						}
						this.suchbegriffOld = this.suchbegriff;
						this.suchbegriff = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Suchbegriff);
				  }
			}

			[DataMember]
			public OrganisationTypeDto OrganisationType {
				  get { return this.organisationType; }
				  set
				  {
						if (this.organisationType == value)
						{
							  return;
						}

						if (this.organisationType != value)
						{
							  this.IsDirty = true;
						}
						this.organisationTypeOld = this.organisationType;
						this.organisationType = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => OrganisationType);
				  }
			}

			[DataMember]
			public AddressDto Adresse {
				  get { return this.adresse; }
				  set
				  {
						if (this.adresse == value)
						{
							  return;
						}

						if (this.adresse != value)
						{
							  this.IsDirty = true;
						}
						this.adresseOld = this.adresse;
						this.adresse = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Adresse);
				  }
			}

            [DataMember]
            public string Businesstyp {
                get { return this.businesstyp; }
                set {
                    if (this.businesstyp == value) {
                        return;
                    }

                    if (this.businesstyp != value) {
                        this.IsDirty = true;
                    }
                    this.businesstypOld = this.businesstyp;
                    this.businesstyp = value;
                    this.CanUndo = true;
                    RaisePropertyChanged(() => Businesstyp);
                }
            }

            [DataMember]
            public string Organisationbeziehung {
                get { return this.organisationbeziehung; }
                set {
                    if (this.organisationbeziehung == value) {
                        return;
                    }

                    if (this.organisationbeziehung != value) {
                        this.IsDirty = true;
                    }
                    this.organisationbeziehungOld = this.organisationbeziehung;
                    this.organisationbeziehung = value;
                    this.CanUndo = true;
                    RaisePropertyChanged(() => Organisationbeziehung);
                }
            }

			[DataMember]
			public TelefonDto Telefon {
				  get { return this.telefon; }
				  set
				  {
						if (this.telefon == value)
						{
							  return;
						}

						if (this.telefon != value)
						{
							  this.IsDirty = true;
						}
						this.telefonOld = this.telefon;
						this.telefon = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Telefon);
				  }
			}

			[DataMember]
			public TelefonDto Fax {
				  get { return this.fax; }
				  set
				  {
						if (this.fax == value)
						{
							  return;
						}

						if (this.fax != value)
						{
							  this.IsDirty = true;
						}
						this.faxOld = this.fax;
						this.fax = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Fax);
				  }
			}

			[DataMember]
			public string eMail {
				  get { return this.email; }
				  set
				  {
						if (this.email == value)
						{
							  return;
						}

						if (this.email != value)
						{
							  this.IsDirty = true;
						}
						this.emailOld = this.email;
						this.email = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => eMail);
				  }
			}

            [DataMember]
            public string Erstkontakt {
                get { return this.erstkontakt; }
                set {
                    if (this.erstkontakt == value) {
                        return;
                    }

                    if (this.erstkontakt != value) {
                        this.IsDirty = true;
                    }
                    this.erstkontaktOld = this.erstkontakt;
                    this.erstkontakt = value;
                    this.CanUndo = true;
                    RaisePropertyChanged(() => Erstkontakt);
                }
            }

            [DataMember]
            public string ErstkontaktMa {
                get { return this.erstkontaktMa; }
                set {
                    if (this.erstkontaktMa == value) {
                        return;
                    }

                    if (this.erstkontaktMa != value) {
                        this.IsDirty = true;
                    }
                    this.erstkontaktMaOld = this.erstkontaktMa;
                    this.erstkontaktMa = value;
                    this.CanUndo = true;
                    RaisePropertyChanged(() => ErstkontaktMa);
                }
            }

            [DataMember]
            public DateTime ErstkontaktDatum {
                get { return this.erstkontaktDatum; }
                set {
                    if (this.erstkontaktDatum == value) {
                        return;
                    }

                    if (this.erstkontaktDatum != value) {
                        this.IsDirty = true;
                    }
                    this.erstkontaktDatumOld = this.erstkontaktDatum;
                    this.erstkontaktDatum = value;
                    this.CanUndo = true;
                    RaisePropertyChanged(() => ErstkontaktDatum);
                }
            }

            [DataMember]
            public string Kontaktquelle {
                get { return this.kontaktquelle; }
                set {
                    if (this.kontaktquelle == value) {
                        return;
                    }

                    if (this.kontaktquelle != value) {
                        this.IsDirty = true;
                    }
                    this.kontaktquelleOld = this.kontaktquelle;
                    this.kontaktquelle = value;
                    this.CanUndo = true;
                    RaisePropertyChanged(() => Kontaktquelle);
                }
            }

            [DataMember]
            public string Kontaktgeber {
                get { return this.kontaktgeber; }
                set {
                    if (this.kontaktgeber == value) {
                        return;
                    }

                    if (this.kontaktgeber != value) {
                        this.IsDirty = true;
                    }
                    this.kontaktgeberOld = this.kontaktgeber;
                    this.kontaktgeber = value;
                    this.CanUndo = true;
                    RaisePropertyChanged(() => Kontaktgeber);
                }
            }

			[DataMember]
			public string Homepage {
				  get { return this.homepage; }
				  set
				  {
						if (this.homepage == value)
						{
							  return;
						}

						if (this.homepage != value)
						{
							  this.IsDirty = true;
						}
						this.homepageOld = this.homepage;
						this.homepage = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Homepage);
				  }
			}

            [DataMember]
            public bool IstEinkauf {
                get { return this.istEinkauf; }
                set {
                    if (this.istEinkauf == value) {
                        return;
                    }

                    if (this.istEinkauf != value) {
                        this.IsDirty = true;
                    }
                    this.istEinkaufOld = this.istEinkauf;
                    this.istEinkauf = value;
                    this.CanUndo = true;
                    RaisePropertyChanged(() => IstEinkauf);
                }
            }

            [DataMember]
            public bool IstLieferant {
                get { return this.istLieferant; }
                set {
                    if (this.istLieferant == value) {
                        return;
                    }

                    if (this.istLieferant != value) {
                        this.IsDirty = true;
                    }
                    this.istLieferantOld = this.istLieferant;
                    this.istLieferant = value;
                    this.CanUndo = true;
                    RaisePropertyChanged(() => IstLieferant);
                }
            }

			[DataMember]
			public string Abteilung {
				  get { return this.abteilung; }
				  set
				  {
						if (this.abteilung == value)
						{
							  return;
						}

						if (this.abteilung != value)
						{
							  this.IsDirty = true;
						}
						this.abteilungOld = this.abteilung;
						this.abteilung = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Abteilung);
				  }
			}

			[DataMember]
			public List<PersonDto> OrganisationsPersons { get; set; }

			public bool HasValue
			{
				  get { return Name != null; }
			}

			public bool IsDirty
			{
				  get { return this.isDirty; }
				  set
				  {
						this.isDirty = value;
						RaisePropertyChanged(() => IsDirty);
				  }
			}

			public bool CanUndo
			{
				  get { return this.canUndo; }
				  set
				  {
						this.canUndo = value;
						RaisePropertyChanged(() => CanUndo);
				  }
			}

			#endregion

			#region C-Tor

			public OrganisationDto(
				  int organisationId,
				  string prefix,
				  string name,
				  string name2,
				  RechtsformDto rechtsform,
				  string suchbegriff,
				  OrganisationTypeDto organisationType,
				  AddressDto adresse,
				  TelefonDto telefon,
				  TelefonDto fax,
				  string email,
				  string homepage,
                  bool istLieferant,
                  bool istEinkauf,
                string erstkontakt,
                DateTime erstkontaktDatum,
                string erstkontaktMa
                )
			{
				  this.OrganisationID = organisationId;
				  this.prefix = prefix;
				  this.name = name;
				  this.name2 = name2;
				  this.rechtsform = rechtsform;
				  this.suchbegriff = suchbegriff;
				  this.organisationType = organisationType;
				  this.adresse = adresse;
				  this.telefon = telefon;
				  this.fax = fax;
				  this.email = email;
				  this.homepage = homepage;
                  this.istLieferant = istLieferant;
                  this.istEinkauf = istEinkauf;
                  this.erstkontakt = erstkontakt;
                  this.erstkontaktDatum = erstkontaktDatum;
                  this.erstkontaktMa = erstkontaktMa;

				  if (Telefon != null)
				  {
						this.Telefon.PropertyChanged += OnPropertyChanged;
				  }
				  if (Fax != null)
				  {
						this.Fax.PropertyChanged += OnPropertyChanged;
				  }
				  if (Adresse != null)
				  {
						this.Adresse.PropertyChanged += OnPropertyChanged;
				  }
			}

			#endregion

			#region Methods

			private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
			{
				  if (e.PropertyName == "TelefonNummer" || e.PropertyName == "Vorwahl" || e.PropertyName == "Durchwahl")
				  {
						this.CanUndo = true;
						this.IsDirty = true;
						RaisePropertyChanged(() => Telefon);
						RaisePropertyChanged(() => Fax);
				  }
				  
				  if (e.PropertyName == "Plz" || e.PropertyName == "Ort" || e.PropertyName == "Strasse" || e.PropertyName == "Zone" || e.PropertyName == "Entfernung" || e.PropertyName == "Hausnummer")
				  {
						this.CanUndo = true;
						this.IsDirty = true;
						RaisePropertyChanged(() => Adresse);
				  }
			}

			public void UndoChanges()
			{
                if (this.CanUndo)
                {
                    this.Prefix = (this.prefixOld == null ? this.Prefix : this.prefixOld);
                    this.Name = (this.nameOld == null ? this.Name : this.nameOld);
                    this.Name2 = (this.name2Old == null ? this.Name2 : this.name2Old);
                    this.Rechtsform = (this.rechtsformOld == null ? this.Rechtsform : this.rechtsformOld);
                    this.Suchbegriff = (this.suchbegriffOld == null ? this.Suchbegriff : this.suchbegriffOld);
                    this.OrganisationType = (this.organisationTypeOld == null ? this.OrganisationType : this.organisationTypeOld);
                    this.Adresse.UndoChanges();
                    this.Telefon.UndoChanges();
                    this.Fax.UndoChanges();
                    this.eMail = (this.emailOld == null ? this.eMail : this.emailOld);
                    this.Homepage = (this.homepageOld == null ? this.Homepage : this.homepageOld);
                    this.Abteilung = (this.abteilungOld == null ? this.Abteilung : this.abteilungOld);
                    this.CanUndo = false;
                }
			}

			#endregion

			#region Overrides

			public override string ToString()
			{
				  return this.Prefix + " " + this.Name + " " + this.Name2 + " " + this.Rechtsform.Name;
			}
			#endregion
	  }
}
